from mySort import *
from PathLength import *


def Reins(Chrom, Selch, ObjV,D):
    """
    重插入子代的新种群,精英选择
    :param Chrom: 父代的种群
    :param Selch: 子代种群
    :param ObjV: 父代适应度
    :return:组合父代与子代后得到的新种群
    """
    NIND = Chrom.shape[0]
    NSel = Selch.shape[0]

    [oldValue, index] = mySort(ObjV)
    oldBestIndex = index[-1]
    oldBestValue = oldValue[-1]

    ObjV1 = PathLength(D, Chrom)
    newBestValue = np.max(ObjV1)

    if oldBestValue > newBestValue:
        newIndex = index[1:NIND - NSel]
        newIndex.append(oldBestIndex)
    else:
        newIndex = index[1:NIND - NSel+1]

    Chrom = np.concatenate((Chrom[newIndex], Selch), axis=0)
    return Chrom